package com.source.sys.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.source.sys.entity.Role;
import com.source.sys.entity.UserRole;
import com.source.sys.service.RoleService;
import com.source.sys.service.UserRoleService;
import com.source.sys.vo.UserRoleVo;
import com.source.utils.CommonResult;
import com.source.utils.ResultEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ZZF
 * @since 2021-02-08
 */
@Api(tags = "用户角色管理(只有update和select)")
@RestController
@RequestMapping("/sys")
public class UserRoleController {
    @Resource
    UserRoleService userRoleService;
    @Resource
    RoleService roleService;



    @ApiOperation(value = "查询所有用户角色(分页)")
    @GetMapping("/user-role/{current}/{size}")
    public CommonResult<Page> listUserRolePage(@PathVariable("current") int current, @PathVariable("size")int size){
        Page<UserRoleVo> listPage = userRoleService.listUserRolePage(current, size);
        if (!listPage.getRecords().isEmpty()){
            return new CommonResult<>(ResultEnum.SUCCESS,listPage);
        }else{
            return new CommonResult<>(ResultEnum.DATA_NOT_FOUND);
        }
    }

    @ApiOperation(value = "回显用户角色信息(修改前操作)")
    @GetMapping(value = "/user-role/{userId}")
    public CommonResult showUserRole(@PathVariable("userId") String userId){
        UserRoleVo userRoleVo = userRoleService.showUserRoleInfo(userId);
        if(userRoleVo != null){
            return new CommonResult(ResultEnum.SUCCESS,userRoleVo);
        }else{
            return new CommonResult(ResultEnum.ERROR);
        }
    }

    @ApiOperation(value = "获取所有可用角色")
    @GetMapping("/role")
    public CommonResult listAllRole(){
        List<Role> roleList = roleService.listRole();
        if(!roleList.isEmpty()){
            return new CommonResult(ResultEnum.SUCCESS,roleList);
        }else{
            return new CommonResult(ResultEnum.ERROR);
        }
    }

    @ApiOperation(value = "修改用户角色信息")
    @PutMapping(value = "/user-role")
    public CommonResult updateUserRole(@RequestBody UserRole userRole){
        boolean updateUserRole = userRoleService.updateUserRole(userRole);
        if(updateUserRole == true){
            //修改成功
            return new CommonResult(ResultEnum.SUCCESS);
        }else{
            return new CommonResult(ResultEnum.ERROR);
        }
    }

    @ApiOperation(value = "模糊查询用户角色", notes = "分页")
    @GetMapping("/query/{value}/page/{pageNo}/{size}")
    public CommonResult likeQueryUserRole(@PathVariable("value") String value,
                                          @PathVariable("pageNo") int pageNo,
                                          @PathVariable("size")int size){
        Page<UserRoleVo> userRoleVoPage = userRoleService.likeSearchUserRolePage(value, pageNo, size);
        if (userRoleVoPage.getTotal() != 0){
            return new CommonResult(ResultEnum.SUCCESS.getCode(),"查询成功",userRoleVoPage);
        }
        return new CommonResult(ResultEnum.ERROR.getCode(),"无匹配记录");

    }
}

